package com.ghxxzx.dagl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghxxzx.dagl.entity.*;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.*;
import com.ghxxzx.dagl.service.JyService;
import com.ghxxzx.dagl.utils.PathUtils;
import com.ghxxzx.dagl.utils.WsFlag;
import com.ghxxzx.dagl.vo.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class JyServiceImpl implements JyService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DajyMapper dajyMapper;
    @Autowired
    DajyClxxMapper dajyClxxMapper;
    @Autowired
    private ClxxMapper clxxMapper;
    @Autowired
    private PathUtils pathUtils;
    @Autowired
    private DaxxMapper daxxMapper;
    @Autowired
    private DaxxWsMapper daxxWsMapper;
    @Autowired
    private ClPathMapper clPathMapper;
    @Autowired
    private WsFlag wsFlag;
    @Autowired
    private DalxMapper dalxMapper;
    @Autowired
    private GryfKjMapper gryfKjMapper;

    public List<LxNode> getDajyClxx(Integer id, String dacl) throws UnsupportedEncodingException {
        QueryWrapper<DajyClxx> dajyClxxQueryWrapper = new QueryWrapper<>();
        ArrayList<LxNode> lxNodes = new ArrayList<>();
        dajyClxxQueryWrapper.eq("dajy_id", id);
        List<DajyClxx> dajyClxxList = dajyClxxMapper.selectList(dajyClxxQueryWrapper);
        ArrayList<Integer> ids = new ArrayList<>();
        for (int i = 0; i < dajyClxxList.size(); i++) {
            ids.add(dajyClxxList.get(i).getClxxId());
        }
        if (ids.size() > 0) {
            QueryWrapper<Clxx> wrapper = new QueryWrapper<>();
            wrapper.in("id", ids);
            List<Clxx> clxxList = clxxMapper.selectList(wrapper);
            for (int i = 0; i < clxxList.size(); i++) {
                Clxx clxx = clxxList.get(i);
                LxNode lxNode = new LxNode();
                lxNode.setId("/" + clxx.getDabh());
                lxNode.setText(clxx.getDabh());
//                if (clxx.getQsym() == null) {
//                    clxx.setQsym("000");
//                }
//                if (clxx.getZzym() == null) {
//                    clxx.setZzym("000");
//                }
//                Integer qsym = Integer.valueOf(clxx.getQsym());
//                Integer zzym = Integer.valueOf(clxx.getZzym());
//                ArrayList<LxNode> nodes = new ArrayList<>();
                LxNode lxNode1 = new LxNode();
                lxNode1.setId("/" + clxx.getWjxh() + clxx.getClmc());
                lxNode1.setText(clxx.getWjxh() + clxx.getClmc());
//                ArrayList<String> yms = new ArrayList<>();
//                for (int j = qsym; j <= zzym; j++) {
//                    String buyema = Buling.Buyema(j);
//                    yms.add(buyema);
//                }
                String path = dacl + "/" + clxx.getTypeName() + lxNode.getId() + lxNode1.getId();
                File file = new File(path);
                if (file != null && file.isDirectory()) {
                    List<LxNode> nodeList = null;
                    try {
                        nodeList = getFile(path);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    lxNode1.setNodes(nodeList);
                    lxNodes.add(lxNode1);
                }
                String pdfPath = dacl + "/" + clxx.getTypeName() + lxNode.getId() + lxNode1.getId() + ".pdf";
                File file1 = new File(pdfPath);
                if (file1 != null && file1.isFile()) {
                    LxNode node = new LxNode(URLEncoder.encode("/" + clxx.getTypeName() + lxNode.getId() + "/" + file1.getName(), "utf-8"), file1.getName());
                    String nodeId = node.getId();
                    String decode = URLDecoder.decode(nodeId, "utf-8");
                    lxNodes.add(node);
                }
            }
        }
        return lxNodes;
    }

    @Override
    public ResultEntity queryJyClxx(Integer id) throws UnsupportedEncodingException {
        Dajy dajy = dajyMapper.findById(id);
        String dacl = pathUtils.getPath();
        ArrayList list = new ArrayList<>();
        LxNode lxNode = new LxNode();
        lxNode.setId(dajy.getDabh());
        lxNode.setText(dajy.getDabh());
        list.add(lxNode);
//        String code = wsFlag.getCode(dajy.getDabh());
//        Integer byCode = dalxMapper.findByCode(code);
        Integer byCode = wsFlag.getTypeId(dajy.getDabh());
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        Integer integer = hashMap.get(byCode);
        ResultEntity resultEntity = null;
        if (integer == 1) {
            List<LxNode> nodeList = getDajyClxx(id, dacl);
            Daxx daxx = daxxMapper.findByDabh(dajy.getDabh());
            lxNode.setNodes(nodeList);
            resultEntity = ResultEntity.ok(new DaxxVo(daxx, list));
        } else if (integer == 2) {
            GryfKj gryfKj = gryfKjMapper.findByDabh(dajy.getDabh());
            String path = dacl + "/" + gryfKj.getTypeName() + "/" + dajy.getDabh();
            List<LxNode> nodes = getFile(path);
            lxNode.setNodes(nodes);
            resultEntity = ResultEntity.ok(new DaxxVo(list, gryfKj));
        } else if (integer == 3) {
            DaxxWs daxxWs = daxxWsMapper.findByDabh(dajy.getDabh());
            List<LxNode> nodeList = getDajyClxx(id, dacl);
            lxNode.setNodes(nodeList);
            resultEntity = ResultEntity.ok(new DaxxWsVo(daxxWs, list));
        }
        return resultEntity;
    }

    private List<LxNode> getFile(String path) throws UnsupportedEncodingException {
        ArrayList<LxNode> nodeList = new ArrayList<>();
        File file = new File(path);
        if (!file.exists()) {
//            throw new JyException("目录材料文件不存在，请联系管理员配置");
            return nodeList;
        }
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file1 = files[i];
            if (file1.isDirectory()) {
//                throw new JyException("这里包含了文件夹");
                List<LxNode> list = getFile(file1.getPath());
                nodeList.add(new LxNode(file1.getName(), file1.getName(), list));
            } else {
                String name = file1.getName();
//                String suffix = name.substring(name.lastIndexOf(".") + 1);
                LxNode lxNode = new LxNode();
                lxNode.setText(name);
                lxNode.setId(URLEncoder.encode(file1.getPath(), "utf-8"));
                nodeList.add(lxNode);
            }
        }
        return nodeList;
    }
//    private List<LxNode> getFile(String path, List<String> yms, String pdf) throws UnsupportedEncodingException {
//        ArrayList<LxNode> nodeList = new ArrayList<>();
//        File file = new File(path);
//        if (!file.exists()) {
//            throw new JyException("目录材料文件不存在，请联系管理员配置");
//        }
//        File[] files = file.listFiles();
//        for (int i = 0; i < files.length; i++) {
//            File file1 = files[i];
//            if (file1.isDirectory()) {
//                throw new JyException("这里包含了文件夹");
//            } else {
//                String name = file1.getName();
////                for (int i1 = 0; i1 < yms.size(); i1++) {
////                    String[] split = name.split("\\.");
////                    String prefix = name.substring(0, name.lastIndexOf("."));
//                String suffix = name.substring(name.lastIndexOf(".") + 1);
////                    if (prefix.equals(yms.get(i1))) {
//                LxNode lxNode = new LxNode();
//                lxNode.setText(name);
//                if (suffix.equals("pdf")) {
//                    lxNode.setId(URLEncoder.encode(pdf + file1.getName(), "utf-8"));
//                } else {
//                    lxNode.setId(URLEncoder.encode(file1.getPath(), "utf-8"));
//                }
//                nodeList.add(lxNode);
////                    }
////                }
//            }
//        }
//        return nodeList;
//    }

    /* private List<LxNode>  getFile(String path,List<LxNode> nodeList){
         File file = new File(path);
         File[] files = file.listFiles();
         for (int i = 0; i < files.length; i++) {
             File file1 = files[i];
             if (file1.isDirectory()){
                 LxNode lxNode2 = new LxNode();
                 lxNode2.setId("/"+file1.getName());
                 lxNode2.setText(file1.getName());
                 List<LxNode> file2 = getFile(file1.getPath(), new ArrayList<LxNode>());
                 lxNode2.setNodes(file2);
                 nodeList.add(lxNode2);
             }else {
                 String name = file1.getName();
                 LxNode lxNode = new LxNode();
                 lxNode.setId("/"+name);
                 lxNode.setText(name);
                 nodeList.add(lxNode);
             }
         }
         return nodeList;
     }*/

    @Override
    public ResultEntity batchdaJy(String[] dabhs, String jyyy, Integer shrId, Integer jysc, Integer id, Integer dalxId) throws ParseException {
        User shruser = userMapper.selectById(shrId);
        User jyruser = userMapper.selectById(id);
        if (shruser == null || jyruser == null) {
            throw new JyException("用户不存在");
        }
        if (dabhs == null || dabhs.length <= 0) {
            throw new JyException("请选择借阅信息");
        }
        if (jysc==null){
            jysc=10;
        }
        if (jyruser.getRoleId() == 2 || jyruser.getRoleId() == 1) {
            Dajy dajy = new Dajy();
            Date date = new Date();
            dajy.setJyr(jyruser.getName());
            dajy.setJyrLxdh(jyruser.getPhone());
            dajy.setJyrId(jyruser.getId());
            dajy.setShr(shruser.getName());
            dajy.setShrId(shruser.getId());
            dajy.setShrLxdh(shruser.getPhone());
            dajy.setSqsj(date);
            dajy.setJysx(jysc);
            HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
            Integer lx = hashMap.get(dalxId);
            if (jyruser.getRoleId() == 2 || jyruser.getRoleId() == 1) {
                Date addDate = addDate(date, dajy.getJysx());
                dajy.setShsj(date);
                dajy.setDqsj(addDate);
                dajy.setZt(1 + "");
                dajy.setJyyy("借阅登记");
            } else {
                dajy.setZt("0");
                dajy.setJyyy(jyyy);
            }
            if (lx == 3) {
                for (String dabh : dabhs) {
                    DaxxWs daxxWs = daxxWsMapper.findByDabh(dabh);
                    dajy.setDabh(daxxWs.getDabh());
                    dajy.setDamc(daxxWs.getWjmc());
                    dajy.setDtslbh(null);
                    Dalx dalx = dalxMapper.selectById(daxxWs.getTypeId());
                    dajy.setDalx(dalx.getDalxName());
                    Integer[] integers = clxxMapper.selectByDabhId(dabh);
                    redoJyJudge(dajy,integers);
                    addDajy(dajy, integers);
                }
            } else if (lx == 1) {
                for (String dabh : dabhs) {
                    Daxx daxx = daxxMapper.findByDabh(dabh);
                    dajy.setDabh(daxx.getDabh());
                    dajy.setDamc(daxx.getXmmc());
                    dajy.setDtslbh(daxx.getDtslbh());
                    Dalx dalx = dalxMapper.selectById(daxx.getTypeId());
                    dajy.setDalx(dalx.getDalxName());
                    Integer[] integers = clxxMapper.selectByDabhId(dabh);
                    redoJyJudge(dajy,integers);
                    addDajy(dajy, integers);
                }
            } else if (lx == 2) {
                for (String dabh : dabhs) {
                    GryfKj gryfKj = gryfKjMapper.findByDabh(dabh);
                    dajy.setDabh(gryfKj.getDabh());
                    dajy.setDamc(gryfKj.getSqcl());
                    dajy.setDtslbh(gryfKj.getSqsx());
                    Dalx dalx = dalxMapper.selectById(gryfKj.getTypeId());
                    dajy.setDalx(dalx.getDalxName());
                    String path = pathUtils.getPath();
                    File file = new File(path + "/" + gryfKj.getTypeName() + "/" + gryfKj.getDabh());
                    if (!file.exists()) {
                        throw new JyException(dajy.getDabh() + ":个人用房没有材料");
                    }
                    redoJyJudge2(dajy);
                    dajyMapper.insert(dajy);
                    DajyClxx dajyClxx = new DajyClxx();
                    dajyClxx.setClxxId(gryfKj.getId());
                    dajyClxx.setDajyId(dajy.getId());
                    dajyClxxMapper.insert(dajyClxx);
                }
            }
        }
        return ResultEntity.ok();
    }

    public void redoJyJudge(Dajy dajy,Integer[] cllxIds){
        QueryWrapper<Dajy> queryWrapper = new QueryWrapper<>();
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(0);
        integers.add(1);
        queryWrapper.in("zt",integers);
        queryWrapper.eq("dabh",dajy.getDabh());
        queryWrapper.eq("jyr_id",dajy.getJyrId());
        queryWrapper.gt("dqsj",new Date());
        queryWrapper.select("id");
        List<Dajy> list = dajyMapper.selectList(queryWrapper);
        if (list==null){
            list=new ArrayList<>();
        }
        integers=new ArrayList<>();
        for (Dajy dajy1 : list) {
            integers.add(dajy1.getId());
        }
        if (integers.size()>0){
            QueryWrapper<DajyClxx> wrapper = new QueryWrapper<>();
            wrapper.in("dajy_id",integers);
            wrapper.select("clxx_id");
            List<DajyClxx> selectList = dajyClxxMapper.selectList(wrapper);
            integers=new ArrayList<>();
            for (DajyClxx dajyClxx : selectList) {
                integers.add(dajyClxx.getClxxId());
            }

        }
        HashSet<Integer> hashSet = new HashSet<>();
        if (cllxIds.length>0){
            for (Integer cllxId : cllxIds) {
                hashSet.add(cllxId);
            }
        }
        for (Integer integer : integers) {
            boolean add = hashSet.add(integer);
            if (!add){
                System.out.println("已借阅id"+integer);
//                throw new JyException("已有材料信息借阅");
            }
        }


    }
    public void redoJyJudge2(Dajy dajy){
        QueryWrapper<Dajy> queryWrapper = new QueryWrapper<>();
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(0);
        integers.add(1);
        queryWrapper.in("zt",integers);
        queryWrapper.eq("dabh",dajy.getDabh());
        queryWrapper.eq("jyr_id",dajy.getJyrId());
        queryWrapper.lt("dqsj",new Date());
        queryWrapper.select("id");
        List<Dajy> list = dajyMapper.selectList(queryWrapper);
        if (list!=null&&list.size()>0){
//            throw new JyException("已有材料信息借阅");
        }

    }
    public void addDajy(Dajy dajy, Integer[] cllxIds) {

        if (cllxIds == null) {
            cllxIds = new Integer[0];
        }
//        String code = wsFlag.getCode(dajy.getDabh());
//        Integer integer = dalxMapper.findByCode(code);
        Integer integer = wsFlag.getTypeId(dajy.getDabh());
        HashMap<Integer, Integer> dalxFlag = wsFlag.getDalxFlag();
        Integer integer1 = dalxFlag.get(integer);
        if (integer1 != 2) {
            redoJyJudge(dajy,cllxIds);
            if (cllxIds.length <= 0) {
                throw new JyException(dajy.getDabh() + ":档案无材料信息");
            }
        }else {
            redoJyJudge2(dajy);
        }

        dajyMapper.insert(dajy);


        for (Integer clxxId : cllxIds) {
            System.out.println(clxxId);
            DajyClxx dajyClxx = new DajyClxx();
            Clxx clxx = clxxMapper.selectById(clxxId);
            String path = pathUtils.getPath();
            File file = new File(path + "/" + clxx.getTypeName() + "/" + clxx.getDabh() + "/" + clxx.getWjxh() + clxx.getClmc());
            File file1 = new File(path + "/" + clxx.getTypeName() + "/" + clxx.getDabh() + "/" + clxx.getWjxh() + clxx.getClmc() + ".pdf");
            if (!file.exists() && !file1.exists()) {
                throw new JyException(dajy.getDabh() + ":" + clxx.getWjxh() + clxx.getClmc() + "没有上传材料");
            }
            dajyClxx.setClxxId(clxxId);
            dajyClxx.setDajyId(dajy.getId());
            dajyClxxMapper.insert(dajyClxx);
        }
    }



    @Override
    public ResultEntity daJy(Dajy dajy, Integer shrId, Integer id, Integer[] clxxIds) throws ParseException {
        User shr = userMapper.selectById(shrId);
        User jyr = userMapper.selectById(id);

        if (shr == null || jyr == null) {
            throw new JyException("用户不存在");
        }
        //问题待优化
        Date date = new Date();
        dajy.setJyr(jyr.getName());
        dajy.setJyrLxdh(jyr.getPhone());
        dajy.setJyrId(jyr.getId());
        dajy.setShr(shr.getName());
        dajy.setShrId(shr.getId());
        dajy.setShrLxdh(shr.getPhone());
        dajy.setSqsj(date);
        if (jyr.getRoleId() == 2 || jyr.getRoleId() == 1) {
            Date addDate = addDate(date, dajy.getJysx());
            dajy.setShsj(date);
            dajy.setDqsj(addDate);
            dajy.setZt(1 + "");
            dajy.setJyyy("借阅登记");
        } else {
            dajy.setZt("0");
        }

        addDajy(dajy, clxxIds);
        return ResultEntity.ok();
    }

    public static Date addDate(Date date, long day) throws ParseException {
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
        time += day; // 相加得到新的毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }

    /**
     * 借阅审核
     *
     * @param keyword
     * @param zt
     * @param pagesize
     * @param pagenumber
     * @param id
     * @return
     */
    @Override
    public ResultEntity queryJysp(String keyword, Integer zt, String sort, Integer pagesize, Integer pagenumber, Integer id) {
        Page<Dajy> dajyPage = new Page<>(pagenumber, pagesize);
        if (id == null) {
            throw new JyException("用户信息不存在");
        }

        QueryWrapper<Dajy> wrapper = new QueryWrapper<>();
        if (zt != null) {
            wrapper.eq("zt", zt.toString());
        }
        wrapper.eq("shr_id", id);
        if (keyword != null && StringUtils.isNotEmpty(keyword)) {
            wrapper.and(w -> w.like("damc", keyword).or().like("dabh", keyword));

        }
        if (sort.equalsIgnoreCase("desc")) {
            wrapper.orderByDesc("sqsj");
        } else {
            wrapper.orderByAsc("sqsj");
        }
        Page<Dajy> selectPage = dajyMapper.selectPage(dajyPage, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setPageNum(pagenumber);
        pageResult.setPageSize(pagesize);
        Long total = selectPage.getTotal();
        pageResult.setTotal(total.intValue());
        pageResult.setData(selectPage.getRecords());
        return ResultEntity.ok(pageResult);
    }


    @Override
    public ResultEntity queryJyspId(String id) {
        Dajy dajy = dajyMapper.selectById(id);
        ArrayList<LxNode> arrayList = new ArrayList<>();
        QueryWrapper<Dalx> dalxQueryWrapper = new QueryWrapper<>();
        dalxQueryWrapper.eq("dalx_name", dajy.getDalx());
        Dalx dalx = dalxMapper.selectOne(dalxQueryWrapper);
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        Integer lx = hashMap.get(dalx.getId());
        if (lx == 1 || lx == 3) {
            QueryWrapper<DajyClxx> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dajy_id", id);
            List<DajyClxx> dajyClxxList = dajyClxxMapper.selectList(queryWrapper);
            ArrayList<Integer> ids = new ArrayList<>();
            for (int i = 0; i < dajyClxxList.size(); i++) {
                ids.add(dajyClxxList.get(i).getClxxId());
            }
            if (ids.size() > 0) {
                QueryWrapper<Clxx> wrapper = new QueryWrapper<>();
                wrapper.in("id", ids);
                List<Clxx> clxxList = clxxMapper.selectList(wrapper);
                for (int i = 0; i < clxxList.size(); i++) {
                    Clxx clxx = clxxList.get(i);
                    arrayList.add(new LxNode(clxx.getId() + "", clxx.getWjxh() + clxx.getClmc()));
                }
            }
        } else if (lx == 2) {
            QueryWrapper<DajyClxx> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dajy_id", id);
            List<DajyClxx> dajyClxxList = dajyClxxMapper.selectList(queryWrapper);
            ArrayList<Integer> ids = new ArrayList<>();
            for (int i = 0; i < dajyClxxList.size(); i++) {
                ids.add(dajyClxxList.get(i).getClxxId());
            }
            if (ids.size() > 0) {
                QueryWrapper<GryfKj> wrapper = new QueryWrapper<>();
                wrapper.in("id", ids);
                List<GryfKj> gryfKjs = gryfKjMapper.selectList(wrapper);
                for (int i = 0; i < gryfKjs.size(); i++) {
                    GryfKj gryfKj = gryfKjs.get(i);
                    arrayList.add(new LxNode(gryfKj.getId() + "", gryfKj.getSqcl()));
                }
            }
        }

        return ResultEntity.ok(new JyhxVo(arrayList, dajy));
    }

    public String getStringDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String s = format.format(date);
        return s;
    }

    @Override
    public ResultEntity queryJy(String keyword, Integer zt, String sort, Integer pagesize, Integer pagenumber, Integer id) {
        QueryWrapper<Dajy> wrapper = new QueryWrapper<>();
        if (id == null) {
            throw new JyException("用户信息不存在");

        }
        if (zt != null) {
            wrapper.eq("zt", zt.toString());
        }
        wrapper.ne("zt", "2");
        wrapper.ne("zt", "3");
        wrapper.eq("jyr_id", id);
        if (keyword != null && StringUtils.isNotEmpty(keyword)) {
            wrapper.and(w -> w.like("damc", keyword).or().like("dabh", keyword));
        }
        if (sort.equalsIgnoreCase("desc")) {
            wrapper.orderByDesc("id");
        } else {
            wrapper.orderByAsc("id");
        }
        Page<Dajy> dajyPage = new Page<>(pagenumber, pagesize);
        Page<Dajy> selectPage = dajyMapper.selectPage(dajyPage, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setPageNum(pagenumber);
        pageResult.setPageSize(pagesize);
        Long total = selectPage.getTotal();
        pageResult.setTotal(total.intValue());
        ArrayList<DajyQueryVo> dajyQueryVos = new ArrayList<>();
        List<Dajy> records = selectPage.getRecords();
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        for (Dajy record : records) {
            String dabh = record.getDabh();
            Integer byCode = wsFlag.getTypeId(dabh);
            Integer lx = hashMap.get(byCode);
            DajyQueryVo dajyQueryVo = new DajyQueryVo();
            dajyQueryVo.setDabh(record.getDabh());
            dajyQueryVo.setAjtm(record.getDamc());
            dajyQueryVo.setId(record.getId());
            dajyQueryVo.setDalx(record.getDalx());
            dajyQueryVo.setZt(record.getZt());
            if (record.getDqsj()!=null){
                String stringDate = getStringDate(record.getDqsj());
                dajyQueryVo.setJssj(stringDate);
            }
            if (lx == 3) {
                DaxxWs byDabh = daxxWsMapper.findByDabh(dabh);
                dajyQueryVo.setNf(byDabh.getNf() + "");
                dajyQueryVo.setJsdw(byDabh.getDwmc());
            } else if (lx == 1) {
                Daxx byDabh = daxxMapper.findByDabh(dabh);
                dajyQueryVo.setNf(byDabh.getYear() + "");
                dajyQueryVo.setJsdw(byDabh.getJsdw());
                dajyQueryVo.setDtslbh(byDabh.getDtslbh());
            } else if (lx == 2) {
                GryfKj byDabh = gryfKjMapper.findByDabh(dabh);
                dajyQueryVo.setNf(byDabh.getYear() + "");
                dajyQueryVo.setJsdw(byDabh.getTownBsc());
                dajyQueryVo.setDtslbh(byDabh.getSqsx());
            }
            dajyQueryVos.add(dajyQueryVo);
        }

        pageResult.setData(dajyQueryVos);
        return ResultEntity.ok(pageResult);
    }



//    public String getCode(String dabh) {
//        String[] split = dabh.split("-");
//        String s = split[0];
//        String substring = s.substring(0, s.length() - 4);
//        return substring;
//    }

    @Override
    public ResultEntity upanddownImg(List<LxNode> lxNodes, String id, Integer type) {
        ArrayList<String> list = new ArrayList<>();
        getList(list, lxNodes);
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (s.equalsIgnoreCase(id)) {
                index = i;
                break;
            }
        }
        if (type == 0) {
            if (index == 0) {
                return ResultEntity.error("这已经是第一张!无法上一张了");
            }
            return ResultEntity.ok(list.get(index - 1));
        } else {
            if (index == list.size() - 1) {
                return ResultEntity.error("这已经是最后一张!无法下一张了");
            }
            return ResultEntity.ok(list.get(index + 1));
        }
    }

    public void getList(ArrayList arrayList, List<LxNode> lxNodes) {
        for (LxNode lxNode : lxNodes) {
            if (lxNode.getNodes() != null && lxNode.getNodes().size() > 0) {
                getList(arrayList, lxNode.getNodes());
            } else {
                int i = lxNode.getId().lastIndexOf("\\.");
                String suffix = "";
                suffix = lxNode.getId().substring(lxNode.getId().lastIndexOf(".") + 1);
                if (suffix.equalsIgnoreCase("jpg") || suffix.equalsIgnoreCase("bmp") || suffix.equalsIgnoreCase("png")) {
                    arrayList.add(lxNode.getId());
                }
            }
        }
    }


    @Override
    public ResultEntity queryDabhClxx(String dabh) {
        QueryWrapper<Clxx> queryWrapper = new QueryWrapper<>();
        List<Clxx> clxxes = clxxMapper.selectByDabh(dabh);
        return ResultEntity.ok(clxxes);
    }
}
